package br.senai.sc.web.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * Conversor para criar o hash da string informada, geralmente utilizado em campos de senha.
 * 
 * @author Fernando Marquardt &lt;fernando.marquardt@sc.senai.br&gt;
 * @since 26/01/2012
 * @version $Id: HashConverter.java 28 2012-03-07 20:48:20Z fernando.marquardt $
 */
@FacesConverter("br.senai.sc.hidroData.HashConverter")
public class HashConverter implements Converter {

    /**
     * {@inheritDoc}
     */
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (StringUtils.trimToEmpty(value).isEmpty()) {
            return null;
        }

        return DigestUtils.md5Hex(value);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return "";
    }
}